package pers.qianyu.month_202101.date_20210109;

import org.junit.*;

/**
 * 345. 反转字符串中的元音字母
 * https://leetcode-cn.com/problems/reverse-vowels-of-a-string/
 *
 * @author mizzle rain
 * @date 2021-01-09 9:56
 */
public class ReverseVowels {
    public String reverseVowels(String s) {
        boolean[] dict = new boolean[128];
        char[] vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        for (char c : vowels) {
            dict[c] = true;
        }
        int len = s.length();
        char[] arr = s.toCharArray();
        int left = 0, right = len - 1;
        while (left < right) {
            while (left < right && !dict[arr[left]]) {
                left++;
            }
            while (left < right && !dict[arr[right]]) {
                right--;
            }
            char temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
        return new String(arr);
    }

    @Test
    public void test1() {
        String res = new ReverseVowels().reverseVowels("hello");
        Assert.assertEquals(res, "holle");
    }

    @Test
    public void test2() {
        String res = new ReverseVowels().reverseVowels("leetcode");
        Assert.assertEquals(res, "leotcede");
    }
}
